package com.jungle.mvvm.sample.ui.home;

import android.content.res.Configuration;
import android.databinding.Observable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.SimpleImmersionOwner;
import com.gyf.immersionbar.components.SimpleImmersionProxy;
import com.jungle.mvvm.base.BaseFragment;
import com.jungle.mvvm.sample.BR;
import com.jungle.mvvm.sample.R;
import com.jungle.mvvm.sample.databinding.FragmentHomeBinding;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/3/5 17:58
 * 页面头部增加banner，下部为列表，整体下滑刷新
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeViewModel> implements SimpleImmersionOwner {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void initData() {
        viewModel.initData();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void initViewObservable() {
        viewModel.uc.finishRefreshing.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable observable, int i) {
                binding.twinklingRefreshLayout.finishRefreshing();
            }
        });
        viewModel.uc.finishLoadmore.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable observable, int i) {
                binding.twinklingRefreshLayout.finishLoadmore();
            }
        });
    }

    /**
     * 创建Fragment
     *
     * @return HomeFragment
     */
    public static HomeFragment newInstance() {
        HomeFragment fragment;
        synchronized (HomeFragment.class) {
            fragment = new HomeFragment();
        }
        return fragment;
    }

    /**
     * ImmersionBar代理类
     */
    private SimpleImmersionProxy mSimpleImmersionProxy = new SimpleImmersionProxy(this);

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mSimpleImmersionProxy.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSimpleImmersionProxy.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mSimpleImmersionProxy.onHiddenChanged(hidden);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mSimpleImmersionProxy.onConfigurationChanged(newConfig);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSimpleImmersionProxy.onDestroy();
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public boolean immersionBarEnabled() {
        return true;
    }
}
